package com.zhs.gccs.view.gt;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.annimon.stream.Stream;
import com.zhs.gccs.R;
import com.zhs.gccs.model.CheckBoxModel;
import com.zhs.gccs.model.XzqArea;
import com.zhs.gccs.view.BaseView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class DistrictSingleSelectView extends BaseView {
    private TextView tv_cancel;
    private TextView tv_confirm;
    private String userXzq;//
    private List<XzqArea> dataList;
    private RecyclerView rv_items_1;
    private List<XzqArea> items1Datas;
    private XzqItemAdapter items_1Adapter;

    public DistrictSingleSelectView(@NonNull Context context) {
        super(context);
    }

    public DistrictSingleSelectView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public DistrictSingleSelectView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void init(Context context, AttributeSet attrs, int defStyleAttr) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_single_district_select,
                this, true);
        tv_cancel = view.findViewById(R.id.tv_cancel);
        tv_confirm = view.findViewById(R.id.tv_confirm);
        rv_items_1 = view.findViewById(R.id.rv_items_1);
        tv_cancel.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View view) {
//                Stream.of(dataList).forEach(item->item.setSelected(false));
                items_1Adapter.notifyDataSetChanged();
//                popupWindow.dismiss();
                if (dataChangeListener != null) dataChangeListener.dataChange();
            }
        });
        tv_confirm.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View view) {
                if (dataChangeListener != null) dataChangeListener.dismis();
            }
        });

        this.dataList = new ArrayList<>();
        this.items1Datas = new ArrayList<>();
        rv_items_1.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
        items_1Adapter = new XzqItemAdapter(context, items1Datas);
        rv_items_1.setAdapter(items_1Adapter);
        items_1Adapter.setOperationListener(new XzqItemAdapter.OperationListener() {
            @Override
            public void onItemClick(int position) {
//                Stream.of(dataList).forEach(item->item.setSelected(false));
//                CheckBoxModel checkBoxModel = dataList.get(position);
//                checkBoxModel.setSelected(true);//(!checkBoxModel.isSelected());
//                items_1Adapter.notifyDataSetChanged();
//                if (dataChangeListener != null) dataChangeListener.dataChange();
            }
        });
    }


    public void initData(String xzq,List<XzqArea> gtAreas) {
        if (gtAreas == null) return;
        this.userXzq = xzq;
        this.dataList.clear();
        this.dataList.addAll(gtAreas);
        if("00".equals(xzq.substring(0,2))){
            //部级权限--省市区
        }else if("0000".equals(xzq.substring(xzq.length()-4))){
            //省级权限--市区
        }else if("00".equals(xzq.substring(xzq.length()-2))){
            //市级权限--区
        }else{//区县--

        }
        items_1Adapter.notifyDataSetChanged();
    }

    public List<XzqArea> forMethod(List<XzqArea> treeList) {
        List<XzqArea> rootTree = new ArrayList<>();
        for (XzqArea tree : treeList) {
            // 第一步 筛选出最顶级的父节点
            if (this.userXzq.equals(tree.getXzqhpdm()) ) {
                rootTree.add(tree);
            }
            // 第二步 筛选出该父节点下的所有子节点列表
            for (XzqArea node : treeList) {
                if (node.getXzqhpdm().equals(tree.getXzqhdm())) {
                    if (tree.getChildren()==null) {
                        tree.setChildren(new ArrayList<>());
                    }
                    tree.getChildren().add(node);
                }
            }
        }
        return rootTree;
    }

    public void setDataChangeListener(DataChangeListener dataChangeListener) {
        this.dataChangeListener = dataChangeListener;
    }

    private DataChangeListener dataChangeListener;

//    public CheckBoxModel getSelected() {
//        return Stream.of(dataList).filter(i -> i.isSelected()).findFirstOrElse(null);
//    }
//
//    public List<CheckBoxModel> getSelectedItems() {
//        return Stream.of(dataList).filter(i -> i.isSelected()).toList();
//    }

    public interface DataChangeListener{
        void  dataChange();
        void dismis();
    }
}
